package com.xiaojie.controller;

import com.xiaojie.ratelimit.RedisRateLimit;
import org.jcp.xml.dsig.internal.SignerOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wangye
 * @classname RatelimitContorller
 * @date 2021/4/1 9:18
 **/
@RestController
@RequestMapping("/ratelimit")
public class RatelimitContorller {

    @Autowired
    private RedisRateLimit redisRateLimit;

    @RequestMapping(value = "count/test", method = RequestMethod.GET)
    public void countTest() {
        try {
            Long n = redisRateLimit.call(10);
            if (n > 0L) {
                System.out.println(n);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "bucket/test", method = RequestMethod.GET)
    public void bucketTest() {
        try {
            Long n = redisRateLimit.bucketlimit(10L, 1L, 0.01, System.currentTimeMillis(),10000L,5L);
            if (n > 0L) {
                System.out.println(n);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
